import request from '@/utils/request'
import axios from 'axios'
import qs from 'qs'

export function getDataList(data) {
    return request({
        url: '/Common/MyWork',
        method: 'post',
        data: data
    })
}


export function getDataListV1(data) {
    return request({
        url: '/Common/GetDataList',
        method: 'post',
        data: data
    })
}

//通用导出功能
export async function funDownload(InterID, data) {

    //获取下载数据
    const result = await GetexportData({ InterID: InterID, data: data })

    
    if (result.code === 20000) {

        

        import ("@/vendor/Export2Excel").then((excel) => {
            const tHeader = result.data1 //中文名称
            const filterVal = result.data2 //英文名称
            const data = formatJson(filterVal, result.data3.Table);
            var date = new Date();

            excel.export_json_to_excel({
                header: tHeader,
                data,
                filename: result.data4 + date.getTime().toString(),
            });

        });
    }

}


export async function funDownloadv1(result) {

    if (result.code === 20000) {

        

        import ("@/vendor/Export2Excel").then((excel) => {
            const tHeader = result.data1 //中文名称
            const filterVal = result.data2 //英文名称
            const data = formatJson(filterVal, result.data3.Table);
            var date = new Date();

            excel.export_json_to_excel({
                header: tHeader,
                data,
                filename: result.data4 + date.getTime().toString(),
            });

        });
    }

}
export function formatJson(filterVal, formDetail) {
    
    return formDetail.map((v) =>
        filterVal.map((j) => {
            if (j === "timestamp") {
                return parseTime(v[j]);
            } else {
                return v[j];
            }
        })
    );
}


export function GetexportData(data) {
    return request({
        url: '/Common/GetexportData',
        method: 'post',
        data: data
    })
}



export function GetFinanceCloseData(data) {
    return request({
        url: '/Common/GetFinanceCloseData',
        method: 'post',
        data: data
    })
}

//验证登录信息
export function GetUserData(data) {
    return request({
        url: '/Common/GetUserData',
        method: 'post',
        data: data
    })
}

//读取列头
export function GetTableColsData(data) {
    return request({
        url: '/Common/GetTableColsData',
        method: 'post',
        data: data
    })
}


//读取列头
export function GetFormColsData(data) {
    return request({
        url: '/Common/GetTableColsData',
        method: 'post',
        data: data
    })
}


export function getFormDataList(data) {
    return request({
        url: '/Common/GetOpenFromDataList',
        method: 'post',
        data: data
    })
}



export function importData(obj) {

    let _this = this;
    let inputDOM = this.$refs.inputer;
    // 通过DOM取文件数据

    this.file = event.currentTarget.files[0];

    var rABS = false; //是否将文件读取为二进制字符串
    var f = this.file;
    var outdata;
    var reader = new FileReader();
    //if (!FileReader.prototype.readAsBinaryString) {
    FileReader.prototype.readAsBinaryString = function(f) {
        var binary = "";
        var rABS = false; //是否将文件读取为二进制字符串
        var pt = this;
        var wb; //读取完成的数据

        var reader = new FileReader();
        reader.onload = function(e) {
            var bytes = new Uint8Array(reader.result);
            var length = bytes.byteLength;
            for (var i = 0; i < length; i++) {
                binary += String.fromCharCode(bytes[i]);
            }
            //此处引入，用于解析excel
            var XLSX = require("xlsx");
            if (rABS) {
                wb = XLSX.read(btoa(fixdata(binary)), {
                    //手动转化
                    type: "base64"
                });
            } else {
                wb = XLSX.read(binary, {
                    type: "binary"
                });
            }
            outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
            ;
            return arr;
        };
        reader.readAsArrayBuffer(f);
    };
    if (rABS) {
        reader.readAsArrayBuffer(f);
    } else {
        reader.readAsBinaryString(f);
    }


    return outdata
}



export function GetAllAccountData(data) {
    return request({
        url: '/Basic/GetAllAccountData',
        method: 'post',
        data: data
    })
}

export function UpdatePsaawordData(data) {
    return request({
        url: '/Basic/UpdatePsaawordData',
        method: 'post',
        data: data
    })
}

export function GetSysUserData(data) {
    return request({
        url: '/Basic/GetSysUserData',
        method: 'post',
        data: data
    })
}

export function UpdateLeaderData(data) {
    return request({
        url: '/Basic/UpdateLeaderData',
        method: 'post',
        data: data
    })
}


export function GetSearchData(data, token) {
    let headers = {};
    if (token) {
      headers["token"] = token;
    }
    return request({
      url: "/APS/APSData",
      method: "POST",
      data,
      headers,
    });
  }


  export function SaveData(data) {
    return request({
        url: '/Common/GetSaveData',
        method: 'post',
        data: data
    })
}

